home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / perl / 5.10.0 / IO / Handle.pm < prev    next >
Encoding:
Perl POD Document  |  2009-06-26  |  7.5 KB  |  378 lines

  1. package IO::Handle;
  2.  
  3. use 5.006_001;
  4. use strict;
  5. our($VERSION, @EXPORT_OK, @ISA);
  6. use Carp;
  7. use Symbol;
  8. use SelectSaver;
  9. use IO ();    # Load the XS module
  10.  
  11. require Exporter;
  12. @ISA = qw(Exporter);
  13.  
  14. $VERSION = "1.27";
  15. $VERSION = eval $VERSION;
  16.  
  17. @EXPORT_OK = qw(
  18.     autoflush
  19.     output_field_separator
  20.     output_record_separator
  21.     input_record_separator
  22.     input_line_number
  23.     format_page_number
  24.     format_lines_per_page
  25.     format_lines_left
  26.     format_name
  27.     format_top_name
  28.     format_line_break_characters
  29.     format_formfeed
  30.     format_write
  31.  
  32.     print
  33.     printf
  34.     say
  35.     getline
  36.     getlines
  37.  
  38.     printflush
  39.     flush
  40.  
  41.     SEEK_SET
  42.     SEEK_CUR
  43.     SEEK_END
  44.     _IOFBF
  45.     _IOLBF
  46.     _IONBF
  47. );
  48.  
  49. ################################################
  50. ## Constructors, destructors.
  51. ##
  52.  
  53. sub new {
  54.     my $class = ref($_[0]) || $_[0] || "IO::Handle";
  55.     @_ == 1 or croak "usage: new $class";
  56.     my $io = gensym;
  57.     bless $io, $class;
  58. }
  59.  
  60. sub new_from_fd {
  61.     my $class = ref($_[0]) || $_[0] || "IO::Handle";
  62.     @_ == 3 or croak "usage: new_from_fd $class FD, MODE";
  63.     my $io = gensym;
  64.     shift;
  65.     IO::Handle::fdopen($io, @_)
  66.     or return undef;
  67.     bless $io, $class;
  68. }
  69.  
  70. #
  71. # There is no need for DESTROY to do anything, because when the
  72. # last reference to an IO object is gone, Perl automatically
  73. # closes its associated files (if any).  However, to avoid any
  74. # attempts to autoload DESTROY, we here define it to do nothing.
  75. #
  76. sub DESTROY {}
  77.  
  78. ################################################
  79. ## Open and close.
  80. ##
  81.  
  82. sub _open_mode_string {
  83.     my ($mode) = @_;
  84.     $mode =~ /^\+?(<|>>?)$/
  85.       or $mode =~ s/^r(\+?)$/$1</
  86.       or $mode =~ s/^w(\+?)$/$1>/
  87.       or $mode =~ s/^a(\+?)$/$1>>/
  88.       or croak "IO::Handle: bad open mode: $mode";
  89.     $mode;
  90. }
  91.  
  92. sub fdopen {
  93.     @_ == 3 or croak 'usage: $io->fdopen(FD, MODE)';
  94.     my ($io, $fd, $mode) = @_;
  95.     local(*GLOB);
  96.  
  97.     if (ref($fd) && "".$fd =~ /GLOB\(/o) {
  98.     # It's a glob reference; Alias it as we cannot get name of anon GLOBs
  99.     my $n = qualify(*GLOB);
  100.     *GLOB = *{*$fd};
  101.     $fd =  $n;
  102.     } elsif ($fd =~ m#^\d+$#) {
  103.     # It's an FD number; prefix with "=".
  104.     $fd = "=$fd";
  105.     }
  106.  
  107.     open($io, _open_mode_string($mode) . '&' . $fd)
  108.     ? $io : undef;
  109. }
  110.  
  111. sub close {
  112.     @_ == 1 or croak 'usage: $io->close()';
  113.     my($io) = @_;
  114.  
  115.     close($io);
  116. }
  117.  
  118. ################################################
  119. ## Normal I/O functions.
  120. ##
  121.  
  122. # flock
  123. # select
  124.  
  125. sub opened {
  126.     @_ == 1 or croak 'usage: $io->opened()';
  127.     defined fileno($_[0]);
  128. }
  129.  
  130. sub fileno {
  131.     @_ == 1 or croak 'usage: $io->fileno()';
  132.     fileno($_[0]);
  133. }
  134.  
  135. sub getc {
  136.     @_ == 1 or croak 'usage: $io->getc()';
  137.     getc($_[0]);
  138. }
  139.  
  140. sub eof {
  141.     @_ == 1 or croak 'usage: $io->eof()';
  142.     eof($_[0]);
  143. }
  144.  
  145. sub print {
  146.     @_ or croak 'usage: $io->print(ARGS)';
  147.     my $this = shift;
  148.     print $this @_;
  149. }
  150.  
  151. sub printf {
  152.     @_ >= 2 or croak 'usage: $io->printf(FMT,[ARGS])';
  153.     my $this = shift;
  154.     printf $this @_;
  155. }
  156.  
  157. sub say {
  158.     @_ or croak 'usage: $io->say(ARGS)';
  159.     my $this = shift;
  160.     print $this @_, "\n";
  161. }
  162.  
  163. sub getline {
  164.     @_ == 1 or croak 'usage: $io->getline()';
  165.     my $this = shift;
  166.     return scalar <$this>;
  167.  
  168. *gets = \&getline;  # deprecated
  169.  
  170. sub getlines {
  171.     @_ == 1 or croak 'usage: $io->getlines()';
  172.     wantarray or
  173.     croak 'Can\'t call $io->getlines in a scalar context, use $io->getline';
  174.     my $this = shift;
  175.     return <$this>;
  176. }
  177.  
  178. sub truncate {
  179.     @_ == 2 or croak 'usage: $io->truncate(LEN)';
  180.     truncate($_[0], $_[1]);
  181. }
  182.  
  183. sub read {
  184.     @_ == 3 || @_ == 4 or croak 'usage: $io->read(BUF, LEN [, OFFSET])';
  185.     read($_[0], $_[1], $_[2], $_[3] || 0);
  186. }
  187.  
  188. sub sysread {
  189.     @_ == 3 || @_ == 4 or croak 'usage: $io->sysread(BUF, LEN [, OFFSET])';
  190.     sysread($_[0], $_[1], $_[2], $_[3] || 0);
  191. }
  192.  
  193. sub write {
  194.     @_ >= 2 && @_ <= 4 or croak 'usage: $io->write(BUF [, LEN [, OFFSET]])';
  195.     local($\) = "";
  196.     $_[2] = length($_[1]) unless defined $_[2];
  197.     print { $_[0] } substr($_[1], $_[3] || 0, $_[2]);
  198. }
  199.  
  200. sub syswrite {
  201.     @_ >= 2 && @_ <= 4 or croak 'usage: $io->syswrite(BUF [, LEN [, OFFSET]])';
  202.     if (defined($_[2])) {
  203.     syswrite($_[0], $_[1], $_[2], $_[3] || 0);
  204.     } else {
  205.     syswrite($_[0], $_[1]);
  206.     }
  207. }
  208.  
  209. sub stat {
  210.     @_ == 1 or croak 'usage: $io->stat()';
  211.     stat($_[0]);
  212. }
  213.  
  214. ################################################
  215. ## State modification functions.
  216. ##
  217.  
  218. sub autoflush {
  219.     my $old = new SelectSaver qualify($_[0], caller);
  220.     my $prev = $|;
  221.     $| = @_ > 1 ? $_[1] : 1;
  222.     $prev;
  223. }
  224.  
  225. sub output_field_separator {
  226.     carp "output_field_separator is not supported on a per-handle basis"
  227.     if ref($_[0]);
  228.     my $prev = $,;
  229.     $, = $_[1] if @_ > 1;
  230.     $prev;
  231. }
  232.  
  233. sub output_record_separator {
  234.     carp "output_record_separator is not supported on a per-handle basis"
  235.     if ref($_[0]);
  236.     my $prev = $\;
  237.     $\ = $_[1] if @_ > 1;
  238.     $prev;
  239. }
  240.  
  241. sub input_record_separator {
  242.     carp "input_record_separator is not supported on a per-handle basis"
  243.     if ref($_[0]);
  244.     my $prev = $/;
  245.     $/ = $_[1] if @_ > 1;
  246.     $prev;
  247. }
  248.  
  249. sub input_line_number {
  250.     local $.;
  251.     () = tell qualify($_[0], caller) if ref($_[0]);
  252.     my $prev = $.;
  253.     $. = $_[1] if @_ > 1;
  254.     $prev;
  255. }
  256.  
  257. sub format_page_number {
  258.     my $old;
  259.     $old = new SelectSaver qualify($_[0], caller) if ref($_[0]);
  260.     my $prev = $%;
  261.     $% = $_[1] if @_ > 1;
  262.     $prev;
  263. }
  264.  
  265. sub format_lines_per_page {
  266.     my $old;
  267.     $old = new SelectSaver qualify($_[0], caller) if ref($_[0]);
  268.     my $prev = $=;
  269.     $= = $_[1] if @_ > 1;
  270.     $prev;
  271. }
  272.  
  273. sub format_lines_left {
  274.     my $old;
  275.     $old = new SelectSaver qualify($_[0], caller) if ref($_[0]);
  276.     my $prev = $-;
  277.     $- = $_[1] if @_ > 1;
  278.     $prev;
  279. }
  280.  
  281. sub format_name {
  282.     my $old;
  283.     $old = new SelectSaver qualify($_[0], caller) if ref($_[0]);
  284.     my $prev = $~;
  285.     $~ = qualify($_[1], caller) if @_ > 1;
  286.     $prev;
  287. }
  288.  
  289. sub format_top_name {
  290.     my $old;
  291.     $old = new SelectSaver qualify($_[0], caller) if ref($_[0]);
  292.     my $prev = $^;
  293.     $^ = qualify($_[1], caller) if @_ > 1;
  294.     $prev;
  295. }
  296.  
  297. sub format_line_break_characters {
  298.     carp "format_line_break_characters is not supported on a per-handle basis"
  299.     if ref($_[0]);
  300.     my $prev = $:;
  301.     $: = $_[1] if @_ > 1;
  302.     $prev;
  303. }
  304.  
  305. sub format_formfeed {
  306.     carp "format_formfeed is not supported on a per-handle basis"
  307.     if ref($_[0]);
  308.     my $prev = $^L;
  309.     $^L = $_[1] if @_ > 1;
  310.     $prev;
  311. }
  312.  
  313. sub formline {
  314.     my $io = shift;
  315.     my $picture = shift;
  316.     local($^A) = $^A;
  317.     local($\) = "";
  318.     formline($picture, @_);
  319.     print $io $^A;
  320. }
  321.  
  322. sub format_write {
  323.     @_ < 3 || croak 'usage: $io->write( [FORMAT_NAME] )';
  324.     if (@_ == 2) {
  325.     my ($io, $fmt) = @_;
  326.     my $oldfmt = $io->format_name(qualify($fmt,caller));
  327.     CORE::write($io);
  328.     $io->format_name($oldfmt);
  329.     } else {
  330.     CORE::write($_[0]);
  331.     }
  332. }
  333.  
  334. # XXX undocumented
  335. sub fcntl {
  336.     @_ == 3 || croak 'usage: $io->fcntl( OP, VALUE );';
  337.     my ($io, $op) = @_;
  338.     return fcntl($io, $op, $_[2]);
  339. }
  340.  
  341. # XXX undocumented
  342. sub ioctl {
  343.     @_ == 3 || croak 'usage: $io->ioctl( OP, VALUE );';
  344.     my ($io, $op) = @_;
  345.     return ioctl($io, $op, $_[2]);
  346. }
  347.  
  348. # this sub is for compatability with older releases of IO that used
  349. # a sub called constant to detemine if a constant existed -- GMB
  350. #
  351. # The SEEK_* and _IO?BF constants were the only constants at that time
  352. # any new code should just chech defined(&CONSTANT_NAME)
  353.  
  354. sub constant {
  355.     no strict 'refs';
  356.     my $name = shift;
  357.     (($name =~ /^(SEEK_(SET|CUR|END)|_IO[FLN]BF)$/) && defined &{$name})
  358.     ? &{$name}() : undef;
  359. }
  360.  
  361. # so that flush.pl can be deprecated
  362.  
  363. sub printflush {
  364.     my $io = shift;
  365.     my $old;
  366.     $old = new SelectSaver qualify($io, caller) if ref($io);
  367.     local $| = 1;
  368.     if(ref($io)) {
  369.         print $io @_;
  370.     }
  371.     else {
  372.     print @_;
  373.     }
  374. }
  375.  
  376. 1;
  377.